package com.imay.live.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;

import com.imay.live.R;
import com.imay.live.fragment.load.LoadMokeyFragment;
import com.imay.live.fragment.user.MainUserListener;
import com.imay.live.fragment.user.UserBaseFragment;
import com.imay.live.fragment.user.UserFragment;
import com.imay.live.helper.H5Helper;
import com.imay.live.helper.ShareSDKHelper;
import com.imay.live.manager.AccountManager;

/**
 * MainActivity用户登录检测模块
 * Created by Administrator on 2017/8/7 0007.
 */

public abstract class BaseUserActivity extends BaseUpdateActivtiy implements MainUserListener {

    public static final String SHOW_START_STATUS_KEY = "show_start_status";
    public static final int OPEN_START_FRAGMENT = 1;
    public static final int OPEN_USER_FRAGMENT = 2;
    public static int status;

    public static final String CLEAR_DATA = "clear_data";

    public boolean userViewShowing = false;

    protected String h5data; // H5跳转
    private LoadMokeyFragment loadMokeyFragment;
    private UserFragment userFragment;

    protected boolean isMain;

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        boolean clear = intent.getBooleanExtra(CLEAR_DATA, false);
        if (clear) {
            if (!AccountManager.getInstance().isLogined()) {
                if (!userViewShowing) {
                    startUserFragment();
                }
            } else {
                if (userViewShowing) {
                    revertUserView();
                }
                loadData();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public abstract void loadData();

    @Override
    public void loginError() {

    }

    @Override
    public void noLogin() {
        finish();
    }

    @Override
    public void startLoad() {
        if (!isTaskRoot()) {
            return;
        }
        isMain = false;
        bringUserViewToFront();
        loadMokeyFragment = LoadMokeyFragment.getInstance();
        userFragment = UserFragment.getInstance();
        status = getIntent().getIntExtra(SHOW_START_STATUS_KEY, OPEN_START_FRAGMENT);
        addFragment(loadMokeyFragment);
        addFragment(userFragment);
        if (status == BaseUserActivity.OPEN_START_FRAGMENT) {
            frame_user_load.setVisibility(View.VISIBLE);
            frame_user_login.setVisibility(View.GONE);
        } else if (status == BaseUserActivity.OPEN_USER_FRAGMENT) {
            frame_user_load.setVisibility(View.GONE);
            frame_user_login.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void start() {
        initPermission();
        if (AccountManager.getInstance().isLogined()) {
            startMain();
        } else {
            startUserFragment();
        }
    }

    @Override
    public void startUserFragment() {
        bringUserViewToFront();
        container.setClickable(false);
        frame_user_content.setClickable(true);
        addFragment(UserFragment.getInstance());
        frame_user_load.setVisibility(View.GONE);
        frame_user_login.setVisibility(View.VISIBLE);
    }

    @Override
    public void startMain() {
        isMain = true;
        container.setClickable(true);
        frame_user_content.setClickable(false);
        revertUserView();
        loadData();
    }

    @Override
    public boolean isUserShow() {
        return userViewShowing;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case UserBaseFragment.REQUEST_LOGIN:
            case UserBaseFragment.REQUEST_RESIGTER:
                if (resultCode == RESULT_OK) {
                    startMain();
                }
                break;
        }
    }


    /**
     * loading界面
     *
     * @param loadFragment
     */
    private void addFragment(Fragment loadFragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (loadFragment instanceof LoadMokeyFragment) {
            transaction.add(R.id.frame_user_load, loadFragment);
            transaction.show(loadFragment);
        } else if (loadFragment instanceof UserFragment) {
            transaction.add(R.id.frame_user_login, loadFragment);
            transaction.show(loadFragment);
        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * 切换登录或者其他
     */
    public void openFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        fragmentManager.getFragments().remove(loadMokeyFragment);
        fragmentManager.getFragments().add(userFragment);
        if (!userFragment.isAdded()) {
            transaction.add(R.id.frame_user_content, userFragment);
        }
        transaction.show(userFragment);
        transaction.commitNowAllowingStateLoss();
    }

    /**
     * 检测浏览器启动信息
     *
     * @param intent
     */
    private void checkH5Data(Intent intent) {
        h5data = ShareSDKHelper.getDataUrl(intent);
        if (AccountManager.getInstance().isLogined()) {
            H5Helper.startActivity(h5data, this);
            h5data = "";
        } else {
            if (!userViewShowing) {
                startUserFragment();
            }
        }
    }

    /**
     * CallBackActivity启动信息
     *
     * @param intent
     */
    protected void checkWebCallBackData(Intent intent) {
        h5data = intent.getStringExtra(H5Helper.H5REDIRECTION_KEY);
        if (!AccountManager.getInstance().isLogined()) {
            if (!userViewShowing) {
                startUserFragment();
            }
        } else {
            if (userViewShowing) {
                revertUserView();
            }
            loadData();
        }
    }

    protected void bringUserViewToFront() {
        if (frame_user_content != null) {
            frame_user_content.setVisibility(View.VISIBLE);
            fl_main.bringChildToFront(frame_user_content);
            fl_main.updateViewLayout(frame_user_content, frame_user_content.getLayoutParams());
            userViewShowing = true;
        }
    }

    protected void revertUserView() {
        frame_user_content.setVisibility(View.GONE);
        fl_main.bringChildToFront(container);
        fl_main.updateViewLayout(container, container.getLayoutParams());
        userViewShowing = false;
    }
}
